﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace crm_library.Extenstions
{
	public static class GenericListExtensions
    {
		public static void Add<T>(this List<T> theList, int index, T item)
			where T: class
		{
			theList.Insert(index, item);
		}
		
        public static void AddIfNotNull<T>(this List<T> theList, T item) where T : class
        {
            if (item != null) theList.Add(item);
        }

        public static void AddRangeIfNotNull<T>(this List<T> theList, params T[] items) where T : class
        {
            foreach (var item in items)
            {
                theList.AddIfNotNull(item);
            }
        }

        public static void AddUniqueIfNotNull<T>(this List<T> theList, T item) where T : class
        {
            if (!theList.Contains(item)) theList.AddIfNotNull(item);
        }

        public static void AddUniqueRangeIfNotNull<T>(this List<T> theList, params T[] items) where T : class
        {
            foreach (var item in items)
            {
                theList.AddUniqueIfNotNull(item);
            }
        }

        public static void AddUnique<T>(this List<T> theList, T item)
        {
            if(!theList.Contains(item)) theList.Add(item);
        }

        public static void AddUniqueRange<T>(this List<T> theList, params T[] items)
        {
            foreach(var item in items)
            {
                theList.AddUnique(item);
            }
        }

        public static void Each<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var item in source)
            {
                action(item);
            }
        }

    }
}

